home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / LSD.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  6KB  |  193 lines

  1. /*
  2. ** LSD - A simple directory lister
  3. ** A public domain C demo program by Bob Stout
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. /* For portability, make everything look like MSC 6 */
  12.  
  13. #if defined(__ZTC__) && __ZTC__ < 0x210
  14.  #include <mflfiles.h>  /* ZTC doesn't do recursive find first/next...  */
  15.  #include <msport.h>    /* ...so use MFLZT library functions            */
  16. #elif defined(__TURBOC__)
  17.  #include <dir.h>
  18.  #define _dos_findfirst(f,a,b) findfirst(f,b,a)
  19.  #define _dos_findnext(b) findnext(b)
  20.  #define find_t ffblk
  21.  #define _A_SUBDIR FA_DIREC
  22.  #define attrib ff_attrib
  23.  #define name ff_name
  24.  #define size ff_size
  25.  #define wr_time ff_time
  26.  #define wr_date ff_date
  27. #else                   /* assume MSC/QC                                */
  28.  #include <dos.h>
  29.  #include <errno.h>
  30. #endif
  31.  
  32. #ifndef SUCCESS
  33.  #define SUCCESS 0
  34. #endif
  35.  
  36. #ifndef __ZTC__         /* these are included with mflfiles.h           */
  37.  #ifdef TRUE
  38.   #undef TRUE
  39.  #endif
  40.  #ifdef FALSE
  41.   #undef FALSE
  42.  #endif
  43.  #ifdef ERROR
  44.   #undef ERROR
  45.  #endif
  46.  enum LOGICAL {FALSE, TRUE};
  47. #endif
  48.  
  49. #ifndef CAST
  50.  #define CAST(new_type,old_object) (*((new_type *)&(old_object)))
  51. #endif
  52.  
  53. struct DOS_TIME {
  54.         unsigned int ss : 5;
  55.         unsigned int mm : 6;
  56.         unsigned int hh : 5;
  57.         } ;
  58. #define dos_time(t) CAST(struct DOS_TIME, (t))
  59.  
  60. struct DOS_DATE {
  61.         unsigned int da : 5;
  62.         unsigned int mo : 4;
  63.         unsigned int yr : 7;
  64.         } ;
  65. #define dos_date(t) CAST(struct DOS_DATE, (t))
  66.  
  67. main(int argc, char *argv[])
  68. {
  69.         int i, files = 0, dirs = 0, argptr = 0, errflag = FALSE, cols;
  70.         long siz_tot = 0L;
  71.         char *p, *fname, *ext, name[13], buf[67], numbuf[12];
  72.         struct find_t ff;
  73.         struct diskfree_t df;
  74.         int one_column(), five_column(), (*display)() = one_column;
  75.         char *sprintfc(char *, long);
  76.  
  77.         strcpy(buf, fname = "*.*");
  78.         if(argc != 1)   for (i = 1; i < argc; ++i)
  79.         {
  80.                 if ('/' == argv[i][0])
  81.                 {
  82.                         if ('W' == toupper(argv[i][1]))
  83.                                 display = five_column;
  84.                         else
  85.                         {
  86.                                 puts("\aUsage: LSD [/W] [file]");
  87.                                 errflag = TRUE;
  88.                         }
  89.                 }
  90.                 else    if (!argptr)    argptr = i;
  91.         }
  92.         if (argptr)
  93.         {
  94.                 fname = argv[argptr];
  95.                 if (!(_dos_findfirst(fname, _A_SUBDIR, &ff)))
  96.                 {
  97.                         if (ff.attrib & _A_SUBDIR)
  98.                                 strcat(strcpy(buf, fname), "\\*.*");
  99.                         else    strcpy(buf, fname);
  100.                 }
  101.                 else    errflag = TRUE;
  102.         }
  103.         if (!errflag && !(_dos_findfirst(buf, 0xff, &ff))) do
  104.         {
  105.                 siz_tot += ff.size;
  106.                 if (ff.attrib & _A_SUBDIR)
  107.                         ++dirs;
  108.                 else    ++files;
  109.                 strcpy(name, ff.name);
  110.                 if (NULL != (p = strchr(name, '.')) && p != name)
  111.                 {
  112.                         *p  = '\0';
  113.                         ext = ++p;
  114.                 }
  115.                 else    ext = "";
  116.                 cols = (*display)(name, ext, ff.size,
  117.                         ff.attrib, ff.wr_date, ff.wr_time);
  118.         } while (SUCCESS == _dos_findnext(&ff));
  119.         else
  120.         {
  121.                 fprintf(stderr, "Cannot do directory on '%s'\n", fname);
  122.                 exit(-1);
  123.         }
  124.         if (cols)
  125.                 fputc('\n', stdout);
  126.         sprintfc(numbuf,siz_tot);
  127.         printf("\n%3d Files totalling %s bytes\n", files, numbuf);
  128.         printf("%3d Director%s\n", dirs, (1 == dirs) ? "y" : "ies");
  129.         _dos_getdiskfree(0, &df);
  130.         sprintfc(numbuf, (long)df.avail_clusters * df.sectors_per_cluster *
  131.                 df.bytes_per_sector);
  132.         printf("%s bytes free\n", numbuf);
  133.         exit(0);
  134. }
  135.  
  136. int one_column(char    *name,
  137.                char    *ext,
  138.                long     size,
  139.                unsigned attribs,
  140.                unsigned date,
  141.                unsigned time)
  142. {
  143.         register int i, mask;
  144.         static char *atr = "RHSVDA";
  145.  
  146.         printf("%-8s %-3s %9ld  ", name, ext, size);
  147.         for (i = 0, mask = 1; i < 6; ++i, mask <<= 1)
  148.                 if (attribs & mask)
  149.                         fputc(atr[i], stdout);
  150.                 else    fputc('.'   , stdout);
  151.         printf("%4d-%02d-%02d%4d:%02d:%02d\n",
  152.                 dos_date(date).mo,
  153.                 dos_date(date).da,
  154.                 (dos_date(date).yr + 80) % 100,
  155.                 dos_time(time).hh,
  156.                 dos_time(time).mm,
  157.                 dos_time(time).ss);
  158.         return 0;
  159. }
  160.  
  161. int five_column(char    *name,
  162.                 char    *ext,
  163.                 long     size,
  164.                 unsigned attribs,
  165.                 unsigned date,
  166.                 unsigned time)
  167. {
  168.         static int cols = 0;
  169.  
  170.         printf("%-8s %-3s%s", name, ext, (5 > ++cols) ? "    " : "");
  171.         if (0 == (cols %= 5))
  172.                 putchar('\n');
  173.         return (cols);
  174. }
  175.  
  176. char *sprintfc(char *string, long num)
  177. {
  178.         if (num > 999999L)
  179.                 sprintf(string, "%d,%03d,%03d",
  180.                         (int)(num / 1000000L),
  181.                         (int)((num % 1000000L) / 1000L),
  182.                         (int)(num % 1000L));
  183.         else
  184.         {
  185.                 if (num > 999L)
  186.                         sprintf(string, "%d,%03d",
  187.                                 (int)(num / 1000L),
  188.                                 (int)(num % 1000L));
  189.                 else sprintf(string, "%d", (int)num);
  190.         }
  191.         return string;
  192. }
  193.